<?php
/**
 * @Notes:【】
 * @Date: 2022-06-05 21:09
 */

namespace App\Notice;

use App\Dao\Settings\SettingsTaskRefreshDao;
use App\Dao\Settings\SettingsTaskTopDao;
use App\Dao\Task\TaskDao;
use App\Dao\User\UserDao;
use App\Model\Task\TaskPayment;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;

class TaskAliPayNotice
{
    public function __exec()
    {
        $data = $this->data;
        dump($data);
        $task_payment_id = $data['out_trade_no'];
        $this->handel($task_payment_id, $data['total_fee']);
    }

    public function notice()
    {
        $params = new \Yurun\PaySDK\AlipayApp\Params\PublicParams;
        $pay_config =  systemConfig(['alipay_appid','alipay_merchant_private_key','alipay_public_key']);
        $params->appID = $pay_config['alipay_appid'];
        $params->appPrivateKey=$pay_config['alipay_merchant_private_key'];
        $params->appPublicKey=$pay_config['alipay_public_key'];
        $pay = new \Yurun\PaySDK\AlipayApp\SDK($params);
        $content = App(RequestInterface::class)->getBody()->getContents();
//        $content = "gmt_create=2022-11-14+14%3A28%3A09&charset=utf-8&seller_email=xiaomubiao20222022%40163.com&subject=450666576443342849&sign=M3WsOyATi%2FLm3Yxild84rftJPQyLtheT1HW%2BIcFtEl6mqwy5u3ODcGay1XG1rgaebBokgw1SIijPhwTD6WwWvGLgsopNKvi9f8s0q%2BX47ttQpiv7QbGyUF%2FquazxRjuTta6cODEObDKeAGMlDjX7dhAueGXTs8mAGG1iwbGj7cqndyyII7HC6kSyH5roZPlbTQVQMtNM81RirlJ6NNarScM7VVQcqGVB%2F68yscjHWyYPETEHOCh19176H19mKGg9QE%2BqGDiZ8XAyT%2Fyqv8gTyyyL4onF581DtymlOgrJh0ZmPlPkTtmjKLlcFfq3ENuz1%2F%2BSQAUnKWOZF7crzxWozQ%3D%3D&buyer_id=2088702133211466&invoice_amount=0.01&notify_id=2022111401222142810011461417399037&fund_bill_list=%5B%7B%22amount%22%3A%220.01%22%2C%22fundChannel%22%3A%22PCREDIT%22%7D%5D&notify_type=trade_status_sync&trade_status=TRADE_SUCCESS&receipt_amount=0.01&app_id=2021003161609450&buyer_pay_amount=0.01&sign_type=RSA2&seller_id=2088541204313207&gmt_payment=2022-11-14+14%3A28%3A09&notify_time=2022-11-14+14%3A28%3A10&version=1.0&out_trade_no=450666576443342849&total_amount=0.01&trade_no=2022111422001411461445840434&auth_app_id=2021003161609450&buyer_logon_id=188****4165&point_amount=0.00";
        parse_str($content,$data);
        if($pay->verifyCallback($data)){
            $this->handel($data['out_trade_no'],$data['total_amount']*100);
        }

    }

    public function getNotifyData()
    {
        return \Yurun\PaySDK\Lib\XML::fromString(App(RequestInterface::class)->getBody()->getContents());
    }


    /**
     * @Inject()
     * @var TaskPayment
     */
    protected $taskPayment;


    /**
     * @Inject()
     * @var TaskDao
     */
    protected $taskDao;

    public function handel($task_payment_id, $total_fee)
    {
        Db::beginTransaction();
        try {
            $taskPayment = $this->taskPayment->where('id', $task_payment_id)->first();

            if ($taskPayment->status == 1) {
                _Exception('已经支付了');
            }
            $taskPayment->status = 1;
            $taskPayment->pay_price = bcdiv($total_fee, 100, 2);
            $taskPayment->save();

            switch ($taskPayment->types) {
                case 'refresh':
                    $num = App(SettingsTaskRefreshDao::class)
                        ->where('id', $taskPayment->refresh_id)
                        ->value('num');
                    App(UserDao::class)->where('uid', $taskPayment->user_id)->increment('refresh_num', $num);
                    break;
                case "top":
                    $num = App(SettingsTaskTopDao::class)
                        ->where('id', $taskPayment->top_id)
                        ->value('num');
                    App(UserDao::class)->where('uid', $taskPayment->user_id)->increment('top_num', $num);
                    break;
                default:
                    $task = App(TaskDao::class)->where('id', $taskPayment->task_id)->first();
                    $task->status = 1;
                    $task->save();
                    break;
            }
            Db::commit();
        } catch (\Exception $exception) {
            Db::rollback();
            _Exception($exception->getMessage());
        }
        return true;
    }
}